import axios from 'axios'

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.baseURL = 'https://dlhtx.zicp.vip/'
axios.defaults.withCredentials = true

export default function request (url, type = 'GET', data = {}, showProgress = true) {
  return new Promise((resolve, reject) => {
    // url = url + '?_=' + new Date().getTime() //防止get请求缓存返回304
    let option = {
      url,
      method: type
    }
    if (type.toLowerCase() === 'get') {
      option.params = data
    } else {
      option.data = data
    }

    axios(option).then(res => { // axios的res被封装了一次 所以真实数据在res.data中
      if (res.data.success) {
        resolve(res.data)
      } else {
        reject(res.data)
      }
    }).catch(err => {
      console.log(err)
    })
  })
}

// request('/auth/login', 'POST', {username: 'hunger', password: '123456'})
//   .then(data=>{
//     console.log(data)
//   })
